package com.wjh.rules.assembler.operator;

import com.wjh.rules.assembler.operator.chain.AbstractOperator;
import com.wjh.rules.assembler.operator.chain.unit.*;
import com.wjh.rules.common.conts.OperatorConts;

public class OperatorChain {

    private static OperatorChain oc = new OperatorChain();

    private AbstractOperator operator;

    private OperatorChain() {
        AbstractOperator equalOperator = new CommonOperator(OperatorConts.EQUAL);
        AbstractOperator notEqualOperator = new CommonOperator(OperatorConts.NOT_EQUAL);
        AbstractOperator containsOperator = new ContainsOperator(OperatorConts.CONTAINS);
        AbstractOperator notContainsOperator = new NotContainsOperator(OperatorConts.NOT_CONTAINS);
        AbstractOperator inOperator = new InOperator(OperatorConts.IN);
        AbstractOperator gtOperator = new CommonOperator(OperatorConts.GT);
        AbstractOperator ltOperator = new CommonOperator(OperatorConts.LT);
        AbstractOperator gteOperator = new CommonOperator(OperatorConts.GTE);
        AbstractOperator lteOperator = new CommonOperator(OperatorConts.LTE);
        AbstractOperator notInOperator = new NotInOperator(OperatorConts.NOT_IN);
        //模糊查询
        AbstractOperator likeOperator = new LikeOperator(OperatorConts.LIKE);
        NotLikeOperator notLikeOperator = new NotLikeOperator(OperatorConts.NOT_LIKE);

        RangeOperator rangeOperator = new RangeOperator(OperatorConts.RANGE);
        NotRangeOperator notRangeOperator = new NotRangeOperator(OperatorConts.NOT_RANGE);
        //---------------------构建chain-------------------------//
        equalOperator.setNextOperator(notEqualOperator);
        notEqualOperator.setNextOperator(containsOperator);
        containsOperator.setNextOperator(notContainsOperator);
        notContainsOperator.setNextOperator(inOperator);
        inOperator.setNextOperator(gtOperator);
        gtOperator.setNextOperator(ltOperator);
        ltOperator.setNextOperator(gteOperator);
        gteOperator.setNextOperator(lteOperator);
        lteOperator.setNextOperator(notInOperator);
        notInOperator.setNextOperator(likeOperator);
        likeOperator.setNextOperator(notLikeOperator);
        notContainsOperator.setNextOperator(rangeOperator);
        rangeOperator.setNextOperator(notRangeOperator);
        this.operator = equalOperator;
    }

    public AbstractOperator getOperator() {
        return this.operator;
    }

    public static AbstractOperator getChain() {
        return oc.getOperator();
    }

}
